import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import cc from "@/components/cat-label/new_file.vue"
import fromItem from '@/components/from-item/from-item.vue'
import lsjUpload from '@/uni_modules/lsj-upload/components/lsj-upload/lsj-upload.vue';
import {
	log
} from 'util'
export default {
	data() {
		return {
			Inv: 0,
			fRecheckresultList: ["复查通过", "复查不通过"],
			appliance: null,
			size: 10,
			debug: true,
			instantly: true, // 选择文件后是否立即自动上传

			// 微信小程序Map对象for循环不显示，所以转成普通数组，不要问为什么，我也不知道
			heights: '156rpx',
			count: 8,
			list: [],
			fileList: [], //文件
			fRecheckfileList: [], //复查资料
			flagNum: -1,
			option: {},

			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式

			// 详情
			totalData: {
				userId: "",
				userName: "",
				companyId: "",
				companyName: "",
				projectId: "",
				projectName: "",
				recordfiles: null,
				fId: "",
				fProjectid: "",
				fProjectname: "",
				fProjectfullname: "",
				fCompanyid: "",
				fCompanyname: "",
				fCompanyfullname: "",
				fCreateuserid: "",
				fCreateusername: "",
				fCreatedate: null,
				fModifyuserid: "",
				fModifydate: null,
				fModifyusername: "",
				fIssueunit: "", //下发指令单位
				fUnittype: "", //单位类型
				fOrdernumber: "", //工作指令编号
				fStartcheckdate: null,
				fStartcheckdates: "", //开始检查日期
				fEndcheckdate: null,
				fEndcheckdates: "", //检查日期结束
				fCheckunit: "", //受检单位1
				fCheckunitid: "", //受检单位id1
				fCheckunit2: "", //受检单位2
				fCheckunit2id: "", //受检单位id2
				fCheckproject: "", //受检项目
				fCheckprojectid: "", //受检项目id
				fProblemdescription: "", //存在的问题概述
				fChangerequirements: "", //整改措施及要求
				fRequesttermdate: null,
				fRequesttermdates: "", //要求整改期限
				fFileurl: "", //附件资料
				fCheckdepartment: "", //检查部门
				fCheckinspectors: "", //检查人员姓名
				fSubsignid1: "", //指定签收人1id
				fSubsignname1: "", //指定签收人1姓名
				// fSubsignid2: "", //指定签收人2id
				// fSubsignname2: "", //指定签收人2姓名
				fCcpeople: "", //抄送人姓名
				fCcpeopleid: "", //抄送人id
				// fCcpeople2: "", //抄送人2姓名
				// fCcpeopleid2: "", //抄送人2id
				fReportid: "", //填报人id'
				fReportname: "", //填报人姓名
				fReportdate: null,
				fReportdates: "", //填报时间
				fSignunit1: "", //受检单位1 签收单位
				fSigndescription1: "", //受检单位1 签收说明
				fSignid1: "", //受检单位1 签收人id
				fSignname1: "", //受检单位1 签收人姓名
				fSigndate1s: "", //受检单位1 签收时间
				// fSignunit2: "", //受检单位2 签收单位
				// fSigndescription2: "", //受检单位2 签收说明
				// fSignid2: "", //受检单位2 签收人id
				// fSignname2: "", //受检单位2 签收人姓名
				// fSigndate2s: "", //受检单位2 签收时间
				fRecheckresult1: "", //受检单位1 复查结果
				fFilenumber1: "", //受检单位1 复查文件编号
				fRecheckdescription1: "", //受检单位1  复查情况说明
				fRecheckfile1: "", //受检单位1 复查相关资料
				fRecheckid1: "", //受检单位1 复查人id
				fRecheckname1: "", //受检单位1 复查人姓名
				fRecheckdate1s: "", //受检单位1 复查完成时间
				// fRecheckresult2: "", //受检单位2 复查结果
				// fFilenumber2: "", //受检单位2 复查文件编号
				// fRecheckdescription2: "", //受检单位2  复查情况说明
				// fRecheckfile2: "", //受检单位2 复查相关资料
				// fRecheckid2: "", //受检单位2 复查人id
				// fRecheckname2: "", //受检单位2 复查人姓名
				// fRecheckdate2s: "", //受检单位2 复查完成时间
				fStatus: "", //信息状态
				fIsdeleted: "",
			},
			userInfo: null,
			must: "must",
			disabled: false,
			recordfiles: [], //删除文件集合
			options: null, //上个页面传递过来的参数
			timing: null, //当前年月日
			//显示隐藏
			basic: 1, //基本信息
			consumption: 1, //材料用量
			close: 2, //闭合申请单
			auditA: 1, //一级审核
			auditB: 1, //二级审核
			//显示隐藏
		};
	},

	onReady() {
		this.$nextTick(() => {
			this.$refs.lsjUpload.show();
		})
		this.fileList = [];
		var that = this
		if (that.totalData.fRecheckfile1 == "" && that.totalData.fRecheckfile1 == null && that.totalData
			.fRecheckfile1 ==
			undefined) {
			console.log("文件么准备好")

		} else {
			setTimeout(() => {
				var folderid = that.totalData.fRecheckfile1
				var userId = that.totalData.userId
				var userName = that.totalData.userName
				var url = that.$http.upload + '?folderid=' + folderid + '&userId=' + userId + '&userName=' +
					userName;
				that.option = {
					// 上传服务器地址，此地址需要替换为你的接口地址
					url: url,
					// 上传附件的key
					name: 'file',

					header: {

					},

					formData: {

					}
				};

			}, 800)
		}

	},
	onLoad(options) {
		var that = this;
		uni.getStorage({ //监督检查方式
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data);

			}
		});

		that.fileList = [];
		that.options = options;
		that.getDetailData(options.fid);
		// || MP-WEIXIN || H5
		// #ifdef APP-PLUS
		this.playVideo()
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5
		this.appliance = 1
		// #endif

	},
	onShow() {

	},
	components: {
		loading,
		cLabel,
		lsjUpload,
		cc,
		fromItem
	},

	onUnload() {
		uni.$off('drawer-page')
	},

	methods: {
		submit() {
			var that = this
			this.Inv = 1

		},
		unsubmit() {
			var that = this
			this.Inv = 0
		},

		//显示隐藏
		downUp(num) {
			if (num == 1) {
				this.basic = 2;
			} else {
				this.basic = 1;
			}
		},
		downUpConsumption(num) {
			if (num == 1) {
				this.consumption = 2;
			} else {
				this.consumption = 1;
			}
		},

		downUpClose(num) {
			if (num == 1) {
				this.close = 2;
			} else {
				this.close = 1;
			}
		},
		downUpAuditA(num) {
			if (num == 1) {
				this.auditA = 2;
			} else {
				this.auditA = 1;
			}
		},
		downUpAuditB(num) {
			if (num == 1) {
				this.auditB = 2;
			} else {
				this.auditB = 1;
			}
		},
		//显示隐藏
		pdfPage(name, url) {
			console.log("打开文件", name, url);
			var aa =
				url
			let links = encodeURIComponent(aa)

			uni.navigateTo({
				url: '/pages/apply/pdf?links=' + links + '&names=' + name,
			})

		},
		// 删除文件
		DelFile(e, id, index) {
			var that = this
			uni.showModal({
				title: '召唤师',
				content: '确定要删除这段回忆吗？',
				cancelText: '再看看',
				confirmText: '再见',
				success: res => {
					if (res.confirm) {

						that.recordfiles.push({
							fId: id
						})
						this.fRecheckfileList.splice(index, 1)
						// this.fileList.splice(e.currentTarget.dataset.index, 1)
					}
				}
			})
		},

		fpersonnameShow() {
			this.$refs.tkitree._show();
		},

		// 上传进度回调
		onprogress(item) {
			if (item.responseText) {
				var data = JSON.parse(item.responseText)
				var url = data[0].atta.fFilepath
				var urlPath;
				if (url.indexOf("D:") != -1) {
					urlPath = url.replace(this.$http.redf, this.$http.file)
				}
				this.fRecheckfileList.push({
					url: urlPath,
					name: item.name,
					fId: data[0].atta.fId
				});

				this.$refs.loading.close();
			}

		},
		// 文件选择回调
		onChange(files) {
			this.$refs.loading.open();

		},



		clickNum(data) {
			this.saveData(data)

		},



		getDetailData(id) {
			this.getDate();
			var that = this;
			that.fileList = [];
			that.fRecheckfileList = [];
			that.recordfiles = [];
			var datas = {
				fId: id,
			}
			that.$http.getWorkOrderExecutionReport2ByFid(datas).then(res => {
				if (res.errcode == 0) {
					this.flagNum = 0;
					that.totalData = res.data.yjQmpMsWrokorderExecutionreport2;
					//复查人
					if (
						this.totalData.fRecheckid1 == "" ||
						this.totalData.fRecheckid1 == null ||
						this.totalData.fRecheckname1 == "" ||
						this.totalData.fRecheckname1 == null
					) {
						this.totalData.fRecheckname1 = this.userInfo.realName;
						this.totalData.fRecheckid1 = this.userInfo.userId;
					}
					// 复查时间1
					if (
						this.totalData.fRecheckdate1s == "" ||
						this.totalData.fRecheckdate1s == null
					) {
						this.totalData.fRecheckdate1s = this.timing;
					}


					// 文件
					var file = res.data.fFileurllist;
					file.map((item, index) => {
						that.fileList.push({
							fId: item.fId,
							name: item.fFileName,
							url: item.url
						})
					})
					// 复核文件
					var Recheckfile = res.data.fRecheckfile1list;
					Recheckfile.map((item, index) => {
						that.fRecheckfileList.push({
							fId: item.fId,
							name: item.fFileName,
							url: item.url
						})
					})

					that.page = true


				} else {
					uni.showToast({
						title: "请求失败",
						icon: 'none',
						duration: 2000
					});
				}

			}, (err) => {
				uni.showToast({
					title: "接口报错",
					icon: 'none',
					duration: 2000
				});
			})

		},

		// 判断必填项
		saveData(data) {

			console.log("fRecheckfileList", this.fRecheckfileList);
			var that = this;
			if (that.totalData.fRecheckresult1 == "" || that.totalData.fRecheckresult1 == undefined) {
				uni.showToast({
					title: '复查情况 复查结果不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (that.totalData.fFilenumber1 == "" || that.totalData.fFilenumber1 ==
				undefined) {
				uni.showToast({
					title: '复查情况 复查文件编号不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (that.fRecheckfileList.length == 0) {
				uni.showToast({
					title: '复查情况 复查相关资料不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (that.totalData.fRecheckname1 == "" || that.totalData.fRecheckname1 ==
				undefined) {
				uni.showToast({
					title: '复查情况 复查人不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else if (that.totalData.fRecheckdate1s == "" || that.totalData.fRecheckdate1s ==
				undefined) {
				uni.showToast({
					title: '复查情况 复查完成时间不能为空',
					icon: 'none',
					duration: 2000
				});
				return false;
			} else {
				let totalData1 = {
					fId: that.totalData.fId, //当前fid
					fRecheckresult1: this.totalData.fRecheckresult1, //受检单位1 复查结果
					fFilenumber1: this.totalData.fFilenumber1, //受检单位1 富察问卷说明
					fRecheckdescription1: this.totalData.fRecheckdescription1, //受检单位1  复查情况说明
					fRecheckfile1: this.totalData.fRecheckfile1, //受检单位1 复查相关资料
					fRecheckid1: this.totalData.fRecheckid1, //受检单位1 复查人id
					fRecheckname1: this.totalData.fRecheckname1, //受检单位1 复查人姓名
					fRecheckdate1s: this.totalData.fRecheckdate1s, //受检单位1 复查完成时间
					recordfiles: this.recordfiles, //删除文件id集合
					isSubmit: data,
				};
				that.$refs.loading.open();
				this.$http.saveWorkOrderExecutionReport2ByRecheck1(totalData1).then(res => {

					if (res.errcode == 0) {

						uni.showToast({
							title: "保存成功",
							icon: 'none',
							duration: 2000
						})
						uni.navigateTo({
							url: '/pages/apply/scene/execute/testing/index'
						});
						this.$refs.loading.close();
					} else {

						uni.showToast({
							title: "保存失败",
							icon: 'none',
							duration: 2000
						});
						this.$refs.loading.close();
					}
				}, (err) => {

					uni.showToast({
						title: "请求失败",
						icon: 'none',
						duration: 2000
					});
					this.$refs.loading.close();
				})

			}
		},

		// 原生窗体按钮显示
		playVideo() {
			let subNVue = uni.getSubNVueById('safe_notice')
			subNVue.show()
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			const hh = (time.getHours() + "").padStart(2, "0");
			const mm = (time.getMinutes() + "").padStart(2, "0");
			const ss = (time.getSeconds() + "").padStart(2, "0");
			// var timer = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
			var timer = `${y}-${m}-${d} ${hh}:${mm}`;
			var timing = `${y}-${m}-${d}`;
			this.timing = `${y}-${m}-${d}`;
			this.time = `${hh}:${mm}`;
			this.lasttiming = `${y}-${m}-${d}`;
			this.lasttime = `${hh}:${mm}`;

		},
		callback() {}
	}
}
